from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import Ui_pelanggan
import pelangganEdit
import res

class Pelanggan(QDialog, Ui_pelanggan.Ui_PelangganDlg):
  def __init__(self, dbConn, dataGlob,  parent=None):
        super(Pelanggan, self).__init__(parent)
        self.setupUi(self)       
        self.initMenu()
        self.tgl_cb.setCheckState(Qt.Checked)
        self.tgl_aw.setDate(QDate.currentDate())
        self.tgl_ak.setDate(QDate.currentDate())
        self.db = dbConn
        self.dataGlob = dataGlob
        self.mastModel = QSqlQueryModel(self)
        self.mastModel.setHeaderData(1, Qt.Horizontal, QVariant("Nama"))
        self.mastModel.setHeaderData(2, Qt.Horizontal, QVariant("No MMC"))
        self.mastModel.setHeaderData(3, Qt.Horizontal, QVariant("Alamat"))
        self.mastModel.setHeaderData(4, Qt.Horizontal, QVariant("Kota"))
        self.mastModel.setHeaderData(5, Qt.Horizontal, QVariant("HP"))
        self.mastModel.setHeaderData(6, Qt.Horizontal, QVariant("Telp"))
        self.mastModel.setHeaderData(7, Qt.Horizontal, QVariant("Tgl Daftar"))
        self.mastModel.setHeaderData(8, Qt.Horizontal, QVariant("Pekerjaan"))
        
        self.masterTv.setModel(self.mastModel)
        self.masterTv.setSelectionMode(QTableView.SingleSelection)
        self.masterTv.setSelectionBehavior(QTableView.SelectRows)        
        print self.dataGlob["nama"]
  @pyqtSignature("")
  def on_namaEdt_editingFinished(self):
      if self.namaEdt.text() != "":
          self.namaCb.setCheckState(Qt.Checked)
      else:
          self.namaCb.setCheckState(Qt.Unchecked)
        
  @pyqtSignature("")
  def on_noEdt_editingFinished(self):
      if self.noEdt.text() != "":
          self.noCb.setCheckState(Qt.Checked)
      else:
          self.noCb.setCheckState(Qt.Unchecked)
          
  @pyqtSignature("")
  def on_tgl_aw_editingFinished(self):
      self.tgl_cb.setCheckState(Qt.Checked)
          
  @pyqtSignature("")
  def on_tgl_ak_editingFinished(self):
      self.tgl_cb.setCheckState(Qt.Checked)
        
  @pyqtSignature("")
  def on_alamat_le_editingFinished(self):
      if self.alamat_le.text() != "":
          self.alamat_cb.setCheckState(Qt.Checked)
      else:
          self.alamat_cb.setCheckState(Qt.Unchecked)
        
  @pyqtSignature("")
  def on_kota_le_editingFinished(self):
      if self.kota_le.text() != "":
          self.kota_cb.setCheckState(Qt.Checked)
      else:
          self.kota_cb.setCheckState(Qt.Unchecked)
        
  @pyqtSignature("")
  def on_pekj_le_editingFinished(self):
      if self.pekj_le.text() != "":
          self.pekj_cb.setCheckState(Qt.Checked)
      else:
          self.pekj_cb.setCheckState(Qt.Unchecked)
        
  def initMenu(self):
      self.retrieveAct = QAction(QIcon(":/retrieve.ico"), self.tr("Retrieve"), self)
#      self.retrieveAct.setShortcut ("Ctrl+R")
      self.connect(self.retrieveAct, SIGNAL("triggered()"), self.retrieveData)
      self.sortAct = QAction(QIcon(":/sort.ico"), self.tr("Sort"), self)
#      self.sortAct.setShortcut ("Ctrl+S")
      self.addAct = QAction(QIcon(":/add.ico"), self.tr("Add"), self)
#      self.addAct.setShortcut ("Ctrl+A")
      self.connect(self.addAct, SIGNAL("triggered()"), self.addData)
      self.editAct = QAction(QIcon(":/edit.ico"), self.tr("Edit"), self)
#      self.editAct.setShortcut ("Ctrl+E")
      self.connect(self.editAct, SIGNAL("triggered()"), self.editData)
      self.delAct = QAction(QIcon(":/del.ico"), self.tr("Delete"), self)
      self.connect(self.delAct, SIGNAL("triggered()"), self.delData)
#      self.delAct.setShortcut ("Ctrl+Del")
      self.printAct = QAction(QIcon("res/print.ico"), self.tr("Print Preview"), self)
#      self.printAct.setShortcut ("Ctrl+P")

  def retrieveData(self):
      qStr = "select id_pelanggan, nama, no_kartu, alamat1, kota1, hp, telp1, tgl_daftar, pekerjaan, kode_pelanggan from pelanggan"
      filt = ""
      urut = " order by nama "
      if self.namaCb.checkState() == Qt.Checked:
          filt = filt + " nama like '"+self.namaEdt.text()+"%' and "
      if self.noCb.checkState() == Qt.Checked:
          filt = filt + " no_kartu like '"+self.noEdt.text()+"%' and "
      if self.tgl_cb.checkState() == Qt.Checked:
          filt = filt + " tgl_daftar between '"+self.tgl_aw.date().toString("yyyy-MM-dd")+"' and '"+self.tgl_ak.date().toString("yyyy-MM-dd")+"' and "
      if self.alamat_cb.checkState() == Qt.Checked:
          filt = filt + " alamat1 like '"+self.alamat_le.text()+"%' and "
      if self.kota_cb.checkState() == Qt.Checked:
          filt = filt + " kota1 like '"+self.kota_le.text()+"%' and "
      if self.pekj_cb.checkState() == Qt.Checked:
          filt = filt + " pekerjaan like '"+self.pekj_le.text()+"%' and "
          
      if filt != "":
          filt = " where "+filt.left(len(filt)-4)
          
      qStr = qStr + filt + urut
      self.mastModel.setQuery(qStr, self.db)
      self.masterTv.setColumnHidden(0, True)
      self.masterTv.setColumnHidden(9, True)
      self.masterTv.setColumnWidth(1, 250)
      self.masterTv.setColumnWidth(2, 100)
      self.masterTv.setColumnWidth(3, 300)
      self.masterTv.setColumnWidth(4, 100)
      self.masterTv.setColumnWidth(5, 100)
      self.masterTv.setColumnWidth(6, 100)
      self.masterTv.setColumnWidth(7, 100)
      self.masterTv.setColumnWidth(8, 150)
      self.masterTv.resizeRowsToContents()
      
  def addData(self):
      form = pelangganEdit.PelangganEdit(self.db, "add",  self.dataGlob, self)
      form.show()

  def editData(self):
      index = self.masterTv.currentIndex()
      if index.isValid():
          brs = self.mastModel.record(index.row())
          id = brs.value("id_pelanggan").toString()
          form = pelangganEdit.PelangganEdit(self.db, id,  self.dataGlob, self)
          form.show()

  def delData(self):
      index = self.masterTv.currentIndex()
      if index.isValid():
          brs = self.mastModel.record(index.row())
          id = brs.value("id_pelanggan").toString()
          msg = brs.value("nama").toString()
          if QMessageBox.question(self, "Pelanggan","Hapus Data : "+msg+"?", QMessageBox.Yes|QMessageBox.No) == QMessageBox.Yes:
              qMast = QSqlQuery(self.db)
              qDet = QSqlQuery(self.db)
              self.db.transaction()
              qDet.exec_("delete from pelanggan_hub where id_pelanggan = '"+id+"'")
              qMast.exec_("delete from pelanggan where id_pelanggan = '"+id+"'")
              self.db.commit()
              self.retrieveData()

  def sortData(self):
       qStr = "select id_pelanggan, kode_pelanggan, nama, no_kartu, alamat1, kota1 from pelanggan"
       self.mastModel.setQuery(qStr, self.db)

  def printData(self):
       qStr = "select id_pelanggan, kode_pelanggan, nama, no_kartu, alamat1, kota1 from pelanggan"
       self.mastModel.setQuery(qStr, self.db)

  def contextMenuEvent(self, event):
        menu = QMenu(self)
        menu.addAction(self.retrieveAct)
        menu.addAction(self.sortAct)
        menu.addSeparator()
        menu.addAction(self.addAct)
        menu.addAction(self.editAct)
        menu.addSeparator()
        menu.addAction(self.delAct)
        menu.addSeparator()
        menu.addAction(self.printAct)
        menu.exec_(event.globalPos())

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = Pelanggan()
    form.show()
    app.exec_()
